/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package menu.tabobject;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import sq.ImageLoader;
import sq.RPG;
import sq.FontLoader;

/** Object to be placed in a MenuTab
 * 
 */
public class MenuTabSlider extends MenuTabImage
{

	public enum Link 
	{ 
		MUSIC_VOL, FX_VOL, NO_LINK 
	}
	
	private static float SLIDER_WIDTH = 280f, SLIDER_VAL_TOP = 20f, SLIDER_PT_WIDTH = 10;
	
	private float valX;
	private boolean clicked;
	private float min, max, value;
	private Link link;
	
	public MenuTabSlider(float min, float max, float value, float x, float y, Alignment align, Link link)
	{

		super(ImageLoader.getSliderPointer(),x,y,align);
		this.setClicked(false);
		this.setMin(min);
		this.setMax(max);
		this.setValue(value);
		this.setValX(ImageLoader.getSliderBG().getWidth()*this.getValue()/(this.getMax()-this.getMin()));
		this.setLink(link);
		
	}
	
	/** Update the game state for a frame.
     * @param input The input object
     * @param delta Time passed since last frame (milliseconds).
     */
	@Override
	public void update(Input input, int delta)
    throws SlickException
    {
    	
    	float mx = input.getMouseX(), my = input.getMouseY();
    	boolean mousedown = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
    	boolean hover = this.isOnObject(mx,my);

    	if (hover) {
    		this.setImage(ImageLoader.getSliderPointerHover());
    		this.setClicked(mousedown);
    	} else {
    		if (!mousedown) {
    			this.setClicked(false);
    		}
    		if (!this.isClicked()) {
    			this.setImage(ImageLoader.getSliderPointer());
    		}
    	}

    	if (this.isClicked()) {
    		this.setImage(ImageLoader.getSliderPointerHover());
    		float width = this.getX()+SLIDER_WIDTH;
    		float minWidth = this.getX();
    		float m_x = mx-SLIDER_PT_WIDTH;
    		this.setValX(((m_x<=minWidth)?minWidth:((m_x>=width)?width:m_x))-this.getX());
    		this.setValue(this.getValX()*(this.getMax()-this.getMin())/SLIDER_WIDTH);
    	}
    		
    }
    
    /** Render the entire screen, so it reflects the current game state.
     * @param g The Slick graphics object, used for drawing.
     */
    @Override
	public void render(Graphics g)
    throws SlickException
    {
    	
    	ImageLoader.getSliderBG().draw(this.getX(), this.getY());
    	float x = this.getX()+this.getValX();
    	this.getImage().draw(x, this.getY());
    	if (this.isClicked()) {
    		g.setFont(FontLoader.getFontSize11());
    		String text = String.valueOf((int)this.getValue());
    		float width = g.getFont().getWidth(text);
    		float tx = x+SLIDER_PT_WIDTH;
    		g.drawString(text, tx-width/2, this.getY()-SLIDER_VAL_TOP);
    	}
    	
    }
    
    @Override
	public void resetValue()
	{
		
		if (this.getLink() == Link.MUSIC_VOL)
		{
			this.setValue(RPG.getSoundSettings().getBackgroundMusicVolume());
		} 
		else if (this.getLink() == Link.FX_VOL)
		{
			this.setValue(RPG.getSoundSettings().getSoundFXVolume());
		}
		this.setValX(this.getValue()*SLIDER_WIDTH);
		
	}

	public float getMin()
	{
		return this.min;
	}

	public void setMin(float min)
	{
		this.min = min;
	}

	public float getMax()
	{
		return this.max;
	}

	public void setMax(float max)
	{
		this.max = max;
	}

	public float getValue()
	{
		return this.value;
	}

	public void setValue(float value)
	{
		this.value = (value<=this.getMin())?this.getMin():((value>=this.getMax())?this.getMax():value);
	} 
    
	private boolean isOnObject(float x, float y)
	{
		boolean dirx = (x >= (this.getX()+this.getValX())) && (x <= (this.getX()+this.getImage().getWidth()+this.getValX()));
		boolean diry = (y >= this.getY()) && (y <= (this.getY()+this.getImage().getHeight()));
		return (dirx && diry);
	}

	private float getValX()
	{
		return valX;
	}

	private void setValX(float valX)
	{
		this.valX = valX;
	}

	private boolean isClicked()
	{
		return clicked;
	}

	private void setClicked(boolean clicked)
	{
		this.clicked = clicked;
	}
	
	public void saveValue()
	{
		
		if (this.getLink() == Link.MUSIC_VOL)
		{
			RPG.getSoundSettings().setBackgroundMusicVolume(this.getValue()/(this.getMax()-this.getMin()));
		} 
		else if (this.getLink() == Link.FX_VOL)
		{
			RPG.getSoundSettings().setSoundFXVolume(this.getValue()/(this.getMax()-this.getMin()));
		}
		
	}

	public Link getLink()
	{
		return this.link;
	}

	public void setLink(Link link) 
	{
		this.link = link;
	}
	
}
